/*年轻人，只管向前看，不要管自暴自弃者的话*/

import 'dart:io';

import 'package:flutter_plugin/util/sputil.dart';
import 'package:get/get.dart';
import 'package:device_info_plus/device_info_plus.dart';

import '../constant/sp_key.dart';
import '../net/http_url.dart';

///初始化注入对象
class Injection extends GetxService {
  // 获取设备唯一编号
  Future<void> initDeviceId() async {
    String deviceId = "";
    final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    try {
      if (Platform.isAndroid) {
        AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
        deviceId = androidInfo.id; // Android
        androidInfo.version;
        print(
            '===========getDeviceId build.serialNumber: ${androidInfo.serialNumber} ===${androidInfo.id} ==aversion:${androidInfo.version.release}');
        SPUtil.instance.put(SPKey.androidVersion, androidInfo.version.release);
      } else if (Platform.isIOS) {
        IosDeviceInfo iosDeviceInfo = await deviceInfoPlugin.iosInfo;
        deviceId = iosDeviceInfo.identifierForVendor!; // iOS
        print('===========getDeviceId build.serialNumber: $iosDeviceInfo  ');
      }
    } catch (e) {
      print('Failed to get device id: $e');
    }
    print('===========getDeviceId deviceId: $deviceId');
    if (deviceId.isNotEmpty) {
      SPUtil.instance.put(SPKey.sn, deviceId);
      HttpUrl.httpHeader[SPKey.sn] = deviceId;
    }
  }
}
